//
//  FC3DAndPLSViewController.m
//  CaiPiao
//
//  Created by apple on 2018/5/22.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "FC3DAndPLSViewController.h"
#import "SelectedNumberView.h"

@interface FC3DAndPLSViewController ()
@property (weak, nonatomic) IBOutlet UILabel *nameAndEndTimeLabel;


@property (weak, nonatomic) IBOutlet UIView *numberBox_1;
@property (weak, nonatomic) IBOutlet UIView *numberBox_2;
@property (weak, nonatomic) IBOutlet UIView *numberBox_3;


@property (nonatomic,retain)NSMutableArray* seletedNumArray_1;
@property (nonatomic,retain)NSMutableArray* seletedNumArray_2;
@property (nonatomic,retain)NSMutableArray* seletedNumArray_3;

@property (nonatomic,retain)NSMutableArray* randomNumberArray_1;
@property (nonatomic,retain)NSMutableArray* randomNumberArray_2;
@property (nonatomic,retain)NSMutableArray* randomNumberArray_3;

@end

@implementation FC3DAndPLSViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.seletedNumArray_1 = [NSMutableArray arrayWithCapacity:0];
    self.seletedNumArray_2 = [NSMutableArray arrayWithCapacity:0];
    self.seletedNumArray_3 = [NSMutableArray arrayWithCapacity:0];
    self.randomNumberArray_1 = [NSMutableArray arrayWithCapacity:0];
    self.randomNumberArray_2 = [NSMutableArray arrayWithCapacity:0];
    self.randomNumberArray_3 = [NSMutableArray arrayWithCapacity:0];
    
    self.title = [SelectionNumManager shared].currentLotteryName;
    
    [self initNumberBoxViews];
    
    [[SelectionNumManager shared] getDataWithLotteryID:[SelectionNumManager shared].currentLotteryID andActionID:@"201" andCallBack:^(BOOL load, NSDictionary *resObj) {
        if (load) {
            NSDictionary* dic = resObj[@"data"];
            self.nameAndEndTimeLabel.text = [NSString stringWithFormat:@"%@ %@",dic[@"Name"],dic[@"BuyEndTime"]];
        }
    }];
}


-(void)initNumberBoxViews{
    NSArray* numberArray = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];
    
    CGFloat ballWidth = 35;
    CGFloat spacingWith = 6;
    NSInteger countBallInRow = self.numberBox_1.frame.size.width/(ballWidth+spacingWith);
    
    __weak typeof(self)weakSelf = self;
    for (int i = 0; i<[numberArray count]; i++) {
        SelectedNumberView* view = [[UINib nibWithNibName:@"NumberView" bundle:[NSBundle mainBundle]] instantiateWithOwner:nil options:nil][0];
        view.frame = CGRectMake((i%countBallInRow)*(ballWidth+spacingWith)+spacingWith, (i/countBallInRow)*(ballWidth+spacingWith), ballWidth, ballWidth);
        view.numberMainColor = [UIColor redColor];
        view.numberLabel.text = numberArray[i];
        view.TapCallback = ^(BOOL selected, NSString *numberText) {
            if (selected) {
                [weakSelf.seletedNumArray_1 addObject:numberText];
            }else{
                [weakSelf.seletedNumArray_1 removeObject:numberText];
            }
        };
        [self.randomNumberArray_1 addObject:view];
        [self.numberBox_1 addSubview:view];
    }
    
    for (int i = 0; i<[numberArray count]; i++) {
        SelectedNumberView* view = [[UINib nibWithNibName:@"NumberView" bundle:[NSBundle mainBundle]] instantiateWithOwner:nil options:nil][0];
        view.frame = CGRectMake((i%countBallInRow)*(ballWidth+spacingWith)+spacingWith, (i/countBallInRow)*(ballWidth+spacingWith), ballWidth, ballWidth);
        view.numberMainColor = [UIColor redColor];
        view.numberLabel.text = numberArray[i];
        view.TapCallback = ^(BOOL selected, NSString *numberText) {
            if (selected) {
                [weakSelf.seletedNumArray_2 addObject:numberText];
            }else{
                [weakSelf.seletedNumArray_2 removeObject:numberText];
            }
        };
        [self.randomNumberArray_2 addObject:view];
        [self.numberBox_2 addSubview:view];
    }
    
    for (int i = 0; i<[numberArray count]; i++) {
        SelectedNumberView* view = [[UINib nibWithNibName:@"NumberView" bundle:[NSBundle mainBundle]] instantiateWithOwner:nil options:nil][0];
        view.frame = CGRectMake((i%countBallInRow)*(ballWidth+spacingWith)+spacingWith, (i/countBallInRow)*(ballWidth+spacingWith), ballWidth, ballWidth);
        view.numberMainColor = [UIColor redColor];
        view.numberLabel.text = numberArray[i];
        view.TapCallback = ^(BOOL selected, NSString *numberText) {
            if (selected) {
                [weakSelf.seletedNumArray_3 addObject:numberText];
            }else{
                [weakSelf.seletedNumArray_3 removeObject:numberText];
            }
        };
        [self.randomNumberArray_3 addObject:view];
        [self.numberBox_3 addSubview:view];
    }
    
}

-(void)clearAction{
    for (SelectedNumberView* view in self.randomNumberArray_1) {
        [view setNormalState];
    }
    for (SelectedNumberView* view in self.randomNumberArray_2) {
        [view setNormalState];
    }
    for (SelectedNumberView* view in self.randomNumberArray_3) {
        [view setNormalState];
    }
}

#pragma mark - 近期开奖
- (IBAction)recentWinNumberAction:(id)sender {
    UIViewController* viewController = [[UIStoryboard storyboardWithName:@"SelectedNumber" bundle:nil] instantiateViewControllerWithIdentifier:@"RecentWinNumberViewController"];
    
    [viewController setPopinTransitionStyle:BKTPopinTransitionStyleCrossDissolve];
    [viewController setPopinTransitionDirection:BKTPopinTransitionDirectionTop];
    
    CGRect presentationRect = CGRectInset(self.view.bounds, 0, 100);
    [self.navigationController presentPopinController:viewController fromRect:presentationRect animated:YES completion:^{
        NSLog(@"Popin presented !");
    }];
}

#pragma mark - 随机选号
- (IBAction)randomAction:(id)sender {
    [self clearAction];
    NSInteger number_1 = arc4random()%10;
    NSInteger number_2 = arc4random()%10;
    NSInteger number_3 = arc4random()%10;
    
    for (SelectedNumberView* view in self.randomNumberArray_1) {
        if ([view.numberLabel.text integerValue]==number_1) {
            [view setHighLightState];
        }
    }

    for (SelectedNumberView* view in self.randomNumberArray_2) {
        if ([view.numberLabel.text integerValue]==number_2) {
            [view setHighLightState];
        }
    }
    for (SelectedNumberView* view in self.randomNumberArray_3) {
        if ([view.numberLabel.text integerValue]==number_3) {
            [view setHighLightState];
        }
    }
}

#pragma mark - 删除
- (IBAction)deleteAction:(id)sender {
    [self clearAction];
}
#pragma mark - 确定
- (IBAction)doneAction:(id)sender {
    if ([OpenLoginManager openLoginPageWithController:self]) {
        return;
    }
    
    if ([self.seletedNumArray_1 count]>0&&[self.seletedNumArray_1 count]>0&&[self.seletedNumArray_1 count]>0) {
        NSString* numberString_1 = [self.seletedNumArray_1 componentsJoinedByString:@","];
        NSString* numberString_2 = [self.seletedNumArray_2 componentsJoinedByString:@","];
        NSString* numberString_3 = [self.seletedNumArray_3 componentsJoinedByString:@","];
        
        NSString* selectedNumberString = [NSString stringWithFormat:@"%@|%@|%@",numberString_1,numberString_2,numberString_3];
        NSLog(@"%@",selectedNumberString);
        CollectionNumberModel *model1 = [CollectionNumberModel setModelWithType:[SelectionNumManager shared].currentLotteryID number:[NSString stringWithFormat:@"%@",selectedNumberString]];
        
        [CollectionNumberManager collectionNumberWithArray:@[model1]];
    }else{
        [EasyTextView showErrorText:@"每位至少选择一个号"];
    }

    
//    [SelectionNumManager shared].currentLotteryName
//    [SelectionNumManager shared].currentLotteryID
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
